package com.liyuan.help_e.service.impl;

import com.liyuan.help_e.dao.CommentDao;
import com.liyuan.help_e.dao.QuestionDao;
import com.liyuan.help_e.dao.ReplyDao;
import com.liyuan.help_e.dao.UserDao;
import com.liyuan.help_e.model.Comment;
import com.liyuan.help_e.model.Reply;
import com.liyuan.help_e.model.User;
import com.liyuan.help_e.service.CommentService;
import com.liyuan.help_e.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.stereotype.Service;

import java.text.SimpleDateFormat;
import java.util.Date;

import static com.liyuan.help_e.component.EnumUtils.*;

@Service
public class CommentServiceImpl implements CommentService {
    @Autowired
    SimpleDateFormat simpleDateFormat;
    @Autowired
    CommentDao commentDao;
    @Autowired
    UserDao userDao;
    @Autowired
    QuestionDao questionDao;
    @Autowired
    UserService userService;
    @Autowired
    ReplyDao replyDao;

    @Override
    public void insertComment(Comment comment) {
        String username = SecurityContextHolder.getContext().getAuthentication().getName();
//        System.out.println(username);
        User user = userService.selectUserByUsername(username);
        comment.setUser(user.getId());
        comment.setCommentCount(0);
        comment.setCreatedAt(simpleDateFormat.format(new Date()));
        comment.setPraiseCount(0);
        commentDao.insertComment(comment);
        Integer commentId = commentDao.selectLastInsertId();
        if(comment.getType().equals(REPLY_QUESTION.getType())){
            questionDao.increCommentCount(comment.getParentId());
            replyDao.insertReply(new Reply(comment.getUser().getId(),commentId,comment.getParentId(),REPLY_QUESTION.getType(),UNREAD.getType(),simpleDateFormat.format(new Date())));
        }else {
            commentDao.increCommentCount(comment.getParentId());
            replyDao.insertReply(new Reply(comment.getUser().getId(),commentId,comment.getParentId(),REPLY_COMMENT.getType(),UNREAD.getType(),simpleDateFormat.format(new Date())));
        }
    }

    @Override
    public Comment[] selectComments(Integer type,Integer parentId) {
        Comment[] comments = commentDao.selectComments(type, parentId);
        for(Comment comment:comments){
            User user  = userDao.selectUserByUserId(comment.getUser().getId());
            comment.getUser().setAvatar_url(user.getAvatar_url());
            comment.getUser().setUsername(user.getUsername());
            comment.getUser().setPassword(user.getPassword());
            comment.getUser().setRole(user.getRole().getId());
        }
        return comments;
    }

    @Override
    public void praise(Integer commentId) {
        commentDao.praise(commentId);
    }

    @Override
    public Comment selectCommentById(Integer id) {
        commentDao.increCommentCount(id);
        Comment comment = commentDao.selectCommentById(id);
        User user=userDao.selectUserByUserId(comment.getUser().getId());
        comment.getUser().setRole(user.getRole().getId());
        comment.getUser().setPassword(user.getPassword());
        comment.getUser().setUsername(user.getUsername());
        comment.getUser().setAvatar_url(user.getAvatar_url());
        return comment;
    }
}
